﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Duet.Web.Core;
using System.IO;
using Intentor.Utilities;

namespace Duet.Web.Util.MasterPages
{
    public partial class Main : Duet.Web.Core.Supertypes.MainMasterPage
    {
        #region Campos

        /// <summary>
        ///     Meta tags da página.
        /// </summary>
        private Dictionary<string, string> _metaTags = new Dictionary<string, string>();

        /// <summary>
        ///     Lista de tags scritps.
        /// </summary>
        private List<HtmlGenericControl> _scritpTags = new List<HtmlGenericControl>();

        /// <summary>
        ///     Objeto que cria as tags de CSS e scritps dinamicamente.
        /// </summary>
        private CreateTagsWebForm createTags = null;

        #endregion

        #region Propriedades

        /// <summary>
        ///     Meta tags da página.
        /// </summary>
        public Dictionary<string, string> MetaTags
        {
            get { return _metaTags; }
            set { _metaTags = value; }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        //protected override void OnInit(EventArgs e)
        //{
        //    this.AddScriptTag("~/Util/Scripts/jquery-1.9.1.min.js");
        //    base.OnInit(e);
        //}


        #endregion
        
        #region Methods

        /// <summary>
        ///     Obtém o <see cref="ScriptManager"/> principal
        ///     da página.
        /// </summary>
        /// <returns><see cref="ScriptManager"/> principal.</returns>
        public override ScriptManager GetMainScriptManager()
        {
            return this.MainScriptManager;
        }

        /// <summary>
        ///     Escreve tags do head ao iniciar a página.
        /// </summary>
        protected void LoadTags()
        {
            if (!IsPostBack)
            {
                this.WriteMetaTags();
            }
        }

        /// <summary>
        ///     Escreve as meta tags na página.
        /// </summary>
        protected void WriteMetaTags()
        {
            foreach (var entry in _metaTags)
            {
                HtmlMeta meta = new HtmlMeta();
                meta.Name = entry.Key;
                meta.Content = entry.Value;

                this.MasterHeader.Controls.Add(meta);
            }
        }

        /// <summary>
        ///     Adiciona caminhos de CSS à página.
        /// </summary>
        public override void WriteLinkCSSTag(string path)
        {
            this.createTags.WriteLinkCSSTag(path);
        }

        /// <summary>
        ///     Escreve caminhos de scripts na página.
        /// </summary>
        public override void WriteScriptTag(string path)
        {
            this.createTags.WriteScriptTag(path);
        }

        /// <summary>
        ///     Adiciona caminhos de scripts à página.
        /// </summary>
        /// <param name="path">
        ///     Caminho relativo do script
        /// </param>
        /// <param name="isSubHeader">
        ///     Se o script deve ser adicionado no holder principal ou no subholder
        /// </param>
        public override void AddScriptTag(string path)
        {
            HtmlGenericControl script = new HtmlGenericControl();
            script.TagName = "script";
            script.Attributes.Add("type", "text/javascript");
            script.Attributes.Add("language", "javascript");
            script.Attributes.Add("src", this.ResolveClientUrl(path));

            this._scritpTags.Add(script);
        }

        /// <summary>
        ///     Exibe caixa de mensagens.
        /// </summary>
        /// <param name="message">Mensagem a ser exibida.</param>
        /// <param name="title">Titulo da caixa de mensagens.</param>
        /// <param name="encloseWithParagraph">Indica se é para colocar parágrafo ao redor do conteúdo.</param>
        public override void ShowMessageBox(string message, string title, bool encloseWithParagraph)
        {
            if (encloseWithParagraph) message = String.Format("<p>{0}</p>", message);

            this.dvDialog.Attributes["title"] = title;
            this.litDialogMessage.Text = message;

            string script =
                "$(document).ready(function() { $(\"#" + this.dvDialog.ClientID + "\").dialog({ bgiframe: true, width: 450, modal: true, resizable: false, draggable: false }); centerDialog(); });";

            ContentManager.RunStartupScript(script, true);
        }

        #endregion
    }
}